﻿package com.athena.controller 
{
	/**
	 * ...
	 * @author Tamnt
	 */
	import flash.display.*;
	import flash.text.TextFormat;
	import flash.events.*;
	import com.athena.vo.ObjectLevelRoom;
	import com.athena.process.* ;
	import com.adobe.serialization.json.JSON;
	
	public class LQAGItemController 
	{
		var movie_mc : MovieClip;
		var objLQAG : Object = new Object() ;
		var formatTextMain:TextFormat = new TextFormat() ;
		
		public function LQAGItemController(_mc:MovieClip, _obj:Object) 
		{
			movie_mc = _mc;
			objLQAG.LQ = _obj.LQ;	
			objLQAG.AG = _obj.AG;	
			movie_mc.LQ_txt.text = BasicFunction.numberFormat(_obj.LQ, 0, false, true) + "LQ" ;
			movie_mc.AG_txt.text = BasicFunction.numberFormat(_obj.AG, 0, false, true) + "AG" ;
			movie_mc.LQ_txt.selectable = false ;
			movie_mc.AG_txt.selectable = false ;
			formatTextMain.font = "Arial" ;
			formatTextMain.bold = true ;
			formatTextMain.size = 14 ;
			formatTextMain.color = 0x00FF00 ;
			movie_mc.LQ_txt.setTextFormat(formatTextMain) ;
			formatTextMain.color = 0xFFFF00 ;
			movie_mc.AG_txt.setTextFormat(formatTextMain) ;
			movie_mc.addEventListener(MouseEvent.CLICK, movie_mc_click);
			movie_mc.BG_mc.gotoAndStop(1);
			movie_mc.addEventListener(MouseEvent.MOUSE_OVER, movie_mc_over);
			movie_mc.addEventListener(MouseEvent.MOUSE_OUT, movie_mc_out);
			movie_mc.buttonMode = true;
		}
		/******************************** Lam tron va hien thi so ***************************************/
		public static function numberFormat(number:Number, maxDecimals:int = 2, forceDecimals:Boolean = false, siStyle:Boolean = true):String {
			var i:int = 0, inc:Number = Math.pow(10, maxDecimals), str:String = String(Math.round(inc * Number(number))/inc);
			var hasSep:Boolean = str.indexOf(",") == -1, sep:int = hasSep ? str.length : str.indexOf(",");
			var ret:String = (hasSep && !forceDecimals ? "" : (siStyle ? "," : ".")) + str.substr(sep+1);
			if (forceDecimals) for (var j:int = 0; j <= maxDecimals - (str.length - (hasSep ? sep-1 : sep)); j++) ret += "0";
			while (i + 3 < (str.substr(0, 1) == "-" ? sep-1 : sep)) ret = (siStyle ? "," : ".") + str.substr(sep - (i += 3), 3) + ret;
			return str.substr(0, sep - i) + ret;
		}
		
		function movie_mc_over(evt:Event)
		{
			movie_mc.BG_mc.gotoAndStop(2);			
		}
		
		function movie_mc_out(evt:Event)
		{
			movie_mc.BG_mc.gotoAndStop(1);
		}
		
		function movie_mc_click(evt:Event)
		{
			var popup : MovieClip = MovieClip(new Popup_Confirm()) ;			
			popup.Title_txt.text = "LQ ==> AG" ;
			popup.Content_txt.text = "Bạn có chắc chắn muốn đổi " + BasicFunction.numberFormat(objLQAG.LQ) + " sang AG?" ;
			popup.OK_btn.addEventListener(MouseEvent.CLICK, OK_Click) ;
			popup.NotOK_btn.addEventListener(MouseEvent.CLICK, NotOK_Click) ;
			popup.name = "ConvertConfirm" ;
			popup.x = 0 ;
			popup.y = 0 ;
			Cocaro.mainStage_mc.addChild(popup) ;
		}
		
		function OK_Click(evt : MouseEvent) {
			var mcRemove : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("ConvertConfirm")) ;
			while (mcRemove != null) {
				Cocaro.mainStage_mc.removeChild(mcRemove) ;
				mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("ConvertConfirm")) ;
			}
			var obj : Object = new Object() ;
			obj.evt = "cv" ;
			obj.LQ = objLQAG.LQ ;
			if (Cocaro.currPlayer.MarkLQ < objLQAG.LQ) {
				BasicFunction.ShowAlert("Bạn không có đủ LQ để đổi sang AG!") ;
				return ;
			}
			obj.AG = objLQAG.AG ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
		}
		
		function NotOK_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("ConvertConfirm")) ;
			while (mc != null) {
				Cocaro.mainStage_mc.removeChild(mc) ;
				mc = MovieClip(Cocaro.mainStage_mc.getChildByName("ConvertConfirm")) ;
			}
		}
	}
}